$(function () {
    allTeacherTable();
    getClassList();
})

function getClassList() {
    $.DemoGet("/classInfo/getClassInfoByUserInfo?token="+sessionStorage.getItem("token"),
        function (data) {
            if(data.success)
            {
                var classListHtml = "";
                for(var i = 0;i<data.obj.length;i++)
                {
                    classListHtml = classListHtml+"<div class=\"col-md-3\"><label class=\"checkbox-inline\"><input type=\"checkbox\"   id='"+data.obj[i]["id"]+"'>"+data.obj[i]["grade_name"]+""+data.obj[i]["class_name"]+"</label></div>";
                }
                $("#classList").html(classListHtml);
            }
            else {
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("获取班级信息失败");
        });
}

function query(params) {
    var queryVal = $("#queryVal").val();
    var param = {
        name:queryVal,
        pageNum : params.offset / params.limit + 1,
        pageSize : params.limit
    };
    return param;
}

/*获取列表数据*/
function allTeacherTable() {
    $("#teacher_list").bootstrapTable({
        url:$.ip+"/teacher/pagelist?token="+sessionStorage.getItem("token"),
        method:'post',
        queryParams:query,
        sidePagination: "server",
        pagination: true,
        striped:true,
        pageNumber:1,
        pageSize: 10,
        pageList: [10, 25, 50, 100],
        dataType:"json",
        contentType:"application/json",
        error:function () {
            layer.msg("获取信息失败");
        },
        responseHandler:function (res) {
            return {
                total:res.obj.total,
                rows :res.obj.list
            }
        },
        columns: [
            {
                title: '序号',
                field: 'id',
                align: 'center',
                valign: 'middle',
                formatter:function (value,row,index){
                    return index+1;
                }
            },
            {
                title: '姓名',
                field: 'teacherName',
                align: 'center',
                valign: 'middle'
            },
            {
                title: '手机号',
                align: 'center',
                field: 'phone',
                valign: 'middle',

            } ,
            {
                title: '出生日期',
                align: 'center',
                field: 'birthdayStr',
                valign: 'middle',

            } ,
            {
                title: '年龄',
                align: 'center',
                field: 'age',
                valign: 'middle',

            } ,
            {
                title: '操作',
                align: 'center',
                field: 'id',
                width:240,
                valign: 'middle',
                formatter:function (id,row) {
                    return '<a onclick="remove(this)" data-id="'+id+'" class="btn btn-danger" href="javascript:;">删除</a> &nbsp<a onclick="update(this)" data-id="'+id+'" class="btn btn-warning " href="javascript:;">编辑</a> &nbsp<a onclick="setUser(this)" data-id="'+id+'" class="btn btn-primary" href="javascript:;">开设账号</a>';
                }
            },
        ]
    });
}


function remove(obj){
    var id = $(obj).data("id");
    $.DemoGet("/teacher/delete/"+id,function (data) {
        if(data.success)
        {
            $("#teacher_list").bootstrapTable('refresh');
        }
        layer.msg(data.msg);
    },function (e) {
        layer.msg("删除失败");
    });
}
function add(){
    $("#teacherName").val("");
    $("#phone").val("");
    $("#time").val("");
    $("[type='checkbox']").each(function ()
    {
        $(this).prop("checked",false);
    });
    popdiv("teacher-info","添加",["50%","50%"],addTeacher)
}

function addTeacher(index,k) {
    var teacherName = $("#teacherName").val();
    var phone = $("#phone").val();
    var birthday = $("#time").val();
    var classList = new Array();
    $.each($('input:checkbox:checked'),function(){
        classList.push(this.id)
    });
    var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
    if(teacherName=='')
    {
        layer.msg("请输入教师姓名");
        return;
    }
    else if(phone!='')
    {
        if (!reg.test(phone)) {
            layer.msg("手机号格式输入有误");
            return;
        }
    }

    $.DemoPost("/teacher/add?token="+sessionStorage.getItem("token"),
        {"teacherName":teacherName,"phone":phone,"birthday":birthday,"classList":classList},
        function (data) {
            if(data.success)
            {
                layer.closeAll();
                $("#teacher_list").bootstrapTable('refresh');
                layer.msg(data.msg);
            }
            else {
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("添加失败");
        });
}
function update(obj){
    var id = $(obj).data("id");
    $("#teacherId").val(id);
    $.DemoGet("/teacher/getTeacherDetail/"+id,function (data) {
        if(data.success)
        {
            $("#teacherName").val(data.obj.teacherName);
            $("#phone").val(data.obj.phone);
            $("#time").val(data.obj.birthdayStr);
            var classList = data.attributes["classList"];
            var isCZ = false;
            $("[type='checkbox']").each(function ()
            {
                isCZ = false;
                for(var i = 0;i<classList.length;i++)
                {

                    if($(this).attr("id")==classList[i]["classId"])
                    {

                       isCZ = true;
                        $(this).prop("checked",true);
                        continue;
                    }
                }
                if(!isCZ)
                {
                    $(this).prop("checked",false);
                }
            })
        }
        else{
            layer.msg(data.msg);
        }
    },function (e) {
       layer.msg("获取详情失败");
    });
    popdiv("teacher-info","修改",["50%","50%"],updataTeacherInfo)
}

function updataTeacherInfo(index,k) {
    var id = $("#teacherId").val();
    var teacherName = $("#teacherName").val();
    var phone = $("#phone").val();
    var birthday = $("#time").val();
    var classList = new Array();
    $.each($('input:checkbox:checked'),function(){
        classList.push(this.id)
    });
    var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/
    if(teacherName=='')
    {
        layer.msg("请输入教师姓名");
        return;
    }
    else if(phone!='')
    {
        if (!reg.test(phone)) {
            layer.msg("手机号格式输入有误");
            return;
        }
    }
    $.DemoPost("/teacher/update",
        {"id":id,"teacherName":teacherName,"phone":phone,"birthday":birthday,"classList":classList},
        function (data) {
            if(data.success)
            {
                layer.msg(data.msg);
                layer.closeAll();
                $("#teacher_list").bootstrapTable('refresh');
            }
            else {
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("添加失败");
        });
}

    function setUser(obj) {
        $("#userName").val("");
        var id = $(obj).data("id");
        $("#mainId").val(id);
        $.DemoGet("/teacher/getUserInfoBySchoolId/"+id,function (data) {
            if(data.success)
            {
                if(data.obj!=null && data.obj.userName!='')
                {
                    $("#userName").val(data.obj.userName);
                    $("#userName").attr("readonly","readonly");
                }
                else{
                    $("#userName").attr("readonly",false);
                }

            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
           layer.msg("获取账户信息失败");
        });
        popdiv("teach-user","开设账号",["50%","30%"],addUserInfo)
    }
    function addUserInfo(index,k) {
        var userName =  $("#userName").val();
        var mainId = $("#mainId").val();
        $.DemoPost("/teacher/addTeacherUserInfo",
            {"userName":userName,"main_id":mainId},
            function (data) {
                if(data.success)
                {
                    layer.close(index);
                    $("#teacher_list").bootstrapTable('refresh');
                }
                layer.msg(data.msg);
            },function (e) {
                layer.msg("添加账号失败");
            });
    }
//s时间
getTime("time");


$("#btnQuery").click(function () {
    $("#teacher_list").bootstrapTable('refresh');
});